//
//  ForgetPassWordViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/11.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "ForgetPassWordViewController.h"

@interface ForgetPassWordViewController ()

@property (strong, nonatomic) UILabel * phoneLabel;
@property (strong, nonatomic) UILabel * codeLabel;
@property (strong, nonatomic) UILabel * passWordLabel;
@property (strong, nonatomic) UILabel * surePassWordLabel;
@property (strong, nonatomic) UITextField * phoneTextFiled;
@property (strong, nonatomic) UITextField * codeTextFiled;
@property (strong, nonatomic) UIButton * codeBtn;
@property (strong, nonatomic) UITextField * PasswordTextField;
@property (strong, nonatomic) UITextField * surePassWordTextField;
@property (strong, nonatomic) UIButton * sureBtn;
@property (strong, nonatomic) UIView * lineView;
@property (strong, nonatomic) UIView * lineView1;
@property (strong, nonatomic) UIView * lineView2;
@property (strong, nonatomic) UIView * lineView3;
@property (nonatomic,assign) int time;
@property (strong, nonatomic) NSTimer * timer;

@end

@implementation ForgetPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.time = 60;
    self.title = @"忘记密码";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backIcon"] style:0 target:self action:@selector(backClick)];
    [self.view addSubview:self.phoneLabel];
    [self.view addSubview:self.phoneTextFiled];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.codeLabel];
    [self.view addSubview:self.codeTextFiled];
    [self.view addSubview:self.codeBtn];
    [self.view addSubview:self.lineView1];
    [self.view addSubview:self.passWordLabel];
    [self.view addSubview:self.PasswordTextField];
    [self.view addSubview:self.lineView2];
    [self.view addSubview:self.surePassWordLabel];
    [self.view addSubview:self.surePassWordTextField];
    [self.view addSubview:self.lineView3];
    [self.view addSubview:self.sureBtn];
}

-(void)backClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(UILabel *)phoneLabel
{
    if (_phoneLabel == nil) {
        _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10 + kStatusAndNavBarH, 75, 30)];
        _phoneLabel.font = font(16);
        _phoneLabel.textColor = SFMColor(51, 51, 51);
        _phoneLabel.textAlignment = NSTextAlignmentLeft;
        _phoneLabel.text = @"手机号";
    }
    return _phoneLabel;
}

-(UITextField *)phoneTextFiled
{
    if (_phoneTextFiled == nil) {
        _phoneTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(90, kStatusAndNavBarH + 10 , WIDTH - 105, 30)];
        _phoneTextFiled.placeholder = @"请输入手机号";
        _phoneTextFiled.font = font(16);
        _phoneTextFiled.textColor = SFMColor(51, 51, 51);
        _phoneTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        _phoneTextFiled.keyboardType = UIKeyboardTypeNumberPad;
        [_phoneTextFiled addTarget:self action:@selector(telPhoneTextChang) forControlEvents:UIControlEventEditingChanged];
    }
    return _phoneTextFiled;
}

-(void)telPhoneTextChang
{
    NSString * str = self.phoneTextFiled.text;
    if (str.length>11) {
        self.phoneTextFiled.text = [str substringToIndex:11];
    }
}

-(UIView *)lineView
{
    if (_lineView == nil) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.phoneTextFiled.frame) + 10, WIDTH - 30, 0.5)];
        _lineView.backgroundColor = SFMLineColor;
    }
    return _lineView;
}

-(UILabel *)codeLabel
{
    if (_codeLabel == nil) {
        _codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.lineView.frame) + 10, 75, 30)];
        _codeLabel.font = font(16);
        _codeLabel.textColor = SFMColor(51, 51, 51);
        _codeLabel.textAlignment = NSTextAlignmentLeft;
        _codeLabel.text = @"验证码";
    }
    return _codeLabel;
}

-(UITextField *)codeTextFiled
{
    if (_codeTextFiled == nil) {
        _codeTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(90, CGRectGetMaxY(self.lineView.frame) + 10, WIDTH - 210, 30)];
        _codeTextFiled.placeholder = @"请输入验证码";
        _codeTextFiled.font = font(16);
        _codeTextFiled.textColor = SFMColor(51, 51, 51);
        _codeTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        _codeTextFiled.keyboardType = UIKeyboardTypeNumberPad;
        [_codeTextFiled addTarget:self action:@selector(codeTextFiledTextChang) forControlEvents:UIControlEventEditingChanged];
    }
    return _codeTextFiled;
}

-(void)codeTextFiledTextChang
{
    NSString * str = self.codeTextFiled.text;
    if (str.length>11) {
        self.codeTextFiled.text = [str substringToIndex:6];
    }
}

-(UIButton *)codeBtn
{
    if (_codeBtn == nil) {
        _codeBtn = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - 110, CGRectGetMaxY(self.lineView.frame) + 12.5, 70, 25)];
        _codeBtn.titleLabel.font = font(12);
        _codeBtn.layer.cornerRadius = 12.5;
        [_codeBtn setTitle:@"获取" forState:UIControlStateNormal];
        [_codeBtn setBackgroundColor:SFMNavBarColor];
        [_codeBtn addTarget:self action:@selector(codeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _codeBtn;
}

#pragma mark - 获取验证码
-(void)codeBtnClick:(UIButton *)btn
{
    [self.view endEditing:YES];
    
    if (self.phoneTextFiled.text.length != 11) {
        [SVProgressHUD ShowErrorMessage:@"手机号码格式有误"];
        return;
    }
    
    if (self.time ==60)
    {
        btn.userInteractionEnabled = NO;
#warning 接口注册
//        NSMutableDictionary * params = [NSMutableDictionary dictionary];
//        [params setValue:self.phoneTextFiled.text forKey:@"mobile"];
//        [params setValue:@"1" forKey:@"type"];
//
//        WeakSelf;
//        [[TNGHTTP shareHttpClient] requestWithPath:@"api/sms" AndParams:params AndReturnBlock:^(NSDictionary *dic) {
//            [SVProgressHUD ShowErrorMessage:@"短信发送成功，请注意查收"];
//            btn.userInteractionEnabled = YES;
//            [weakSelf creatTimer];
//        }AndFailMessage:^(NSString *message, NSString *errorCode) {
//            [MBProgressHUD showMessageWithDelay:message delay:2];
//            btn.userInteractionEnabled = YES;
//        }];
    }else
    {
        if (self.time==0) {
            btn.userInteractionEnabled = YES;
        }
    }
}

#pragma mark - 注册事件
-(void)sureBtnClick:(UIButton *)btn
{
    [self.view endEditing:YES];
    
    if (self.phoneTextFiled.text.length != 11) {
        [SVProgressHUD ShowErrorMessage:@"手机号码格式有误"];
        return;
    }
    
    if (self.codeTextFiled.text.length !=4) {
        [SVProgressHUD ShowErrorMessage:@"验证码输入不正确"];
        return;
    }
    
    if (self.PasswordTextField.text.length<18) {
        [SVProgressHUD ShowErrorMessage:@"密码长度必须6～18位"];
        return;
    }
    
    if (![self.PasswordTextField.text isEqualToString:self.surePassWordTextField.text]) {
        [SVProgressHUD ShowErrorMessage:@"两次输入密码不一致!"];
        return;
    }
    
    BOOL isPhone = [NSString valiMobile:self.phoneTextFiled.text];
    if (isPhone)
    {
        btn.userInteractionEnabled = NO;
#warning 接口注册
//        NSMutableDictionary * params = [NSMutableDictionary dictionary];
//        [params setValue:self.phoneTextFiled.text forKey:@"mobile"];
//        [params setValue:self.passWordLabel.text forKey:@"password"];
//        [params setValue:self.codeTextFiled.text forKey:@"code"];
//        WeakSelf;
//        [[TNGHTTP shareHttpClient] requestWithPath:@"user/register" AndParams:params AndReturnBlock:^(NSDictionary *dic) {
//            [SVProgressHUD ShowSuccessMessage:@"注册成功"];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [weakSelf.navigationController popViewControllerAnimated:YES];
//            });
//        }AndFailMessage:^(NSString *message, NSString *errorCode) {
//            [MBProgressHUD showMessageWithDelay:message delay:2];
//        }];
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)creatTimer
{
    if (self.timer == nil) {
        self.timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setBtnLabel) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
}

#pragma mark -计时器事件
-(void)setBtnLabel
{
    if (self.time >0) {
        self.time--;
        NSString * str =[NSString stringWithFormat:@"%ds",self.time];
        [self.codeBtn setTitle:str forState:UIControlStateNormal];
        [self.codeBtn setTitleColor:SFMColor(207, 207, 207) forState:UIControlStateNormal];
    }
    else
    {
        [self.codeBtn setTitle:@"获取" forState:UIControlStateNormal];
        [self.codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.timer invalidate];
        self.timer = nil;
        self.time = 60;
        self.codeBtn.userInteractionEnabled=YES;
    }
}

-(UIView *)lineView1
{
    if (_lineView1 == nil) {
        _lineView1 = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.codeLabel.frame) + 10, WIDTH - 30, 0.5)];
        _lineView1.backgroundColor = SFMLineColor;
    }
    return _lineView1;
}

-(UILabel *)passWordLabel
{
    if (_passWordLabel == nil) {
        _passWordLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.lineView1.frame) + 10, 75, 30)];
        _passWordLabel.font = font(16);
        _passWordLabel.textColor = SFMColor(51, 51, 51);
        _passWordLabel.textAlignment = NSTextAlignmentLeft;
        _passWordLabel.text = @"新密码";
    }
    return _passWordLabel;
}

-(UITextField *)PasswordTextField
{
    if (_PasswordTextField == nil) {
        _PasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(90, CGRectGetMaxY(self.lineView1.frame) + 10, WIDTH - 105, 30)];
        _PasswordTextField.placeholder = @"请输入新密码";
        _PasswordTextField.font = font(16);
        _PasswordTextField.textColor = SFMColor(51, 51, 51);
        _PasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _PasswordTextField.keyboardType = UIKeyboardTypeNumberPad;
        [_PasswordTextField addTarget:self action:@selector(PasswordTextFieldTextChang) forControlEvents:UIControlEventEditingChanged];
    }
    return _PasswordTextField;
}

-(void)PasswordTextFieldTextChang
{
    NSString * str = self.PasswordTextField.text;
    if (str.length>18) {
        self.PasswordTextField.text = [str substringToIndex:18];
    }
}

-(UIView *)lineView2
{
    if (_lineView2 == nil) {
        _lineView2 = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.passWordLabel.frame) + 10, WIDTH - 30, 0.5)];
        _lineView2.backgroundColor = SFMLineColor;
    }
    return _lineView2;
}

-(UILabel *)surePassWordLabel
{
    if (_surePassWordLabel == nil) {
        _surePassWordLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.lineView2.frame) + 10, 75, 30)];
        _surePassWordLabel.font = font(16);
        _surePassWordLabel.textColor = SFMColor(51, 51, 51);
        _surePassWordLabel.textAlignment = NSTextAlignmentLeft;
        _surePassWordLabel.text = @"确认密码";
    }
    return _surePassWordLabel;
}

-(UITextField *)surePassWordTextField
{
    if (_surePassWordTextField == nil) {
        _surePassWordTextField = [[UITextField alloc] initWithFrame:CGRectMake(90, CGRectGetMaxY(self.lineView2.frame) + 10, WIDTH - 105, 30)];
        _surePassWordTextField.placeholder = @"请输入确认密码";
        _surePassWordTextField.font = font(16);
        _surePassWordTextField.textColor = SFMColor(51, 51, 51);
        _surePassWordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _surePassWordTextField.keyboardType = UIKeyboardTypeNumberPad;
        [_surePassWordTextField addTarget:self action:@selector(surePassWordTextFieldTextChang) forControlEvents:UIControlEventEditingChanged];
    }
    return _surePassWordTextField;
}

-(void)surePassWordTextFieldTextChang
{
    NSString * str = self.surePassWordTextField.text;
    if (str.length>18) {
        self.surePassWordTextField.text = [str substringToIndex:18];
    }
}

-(UIView *)lineView3
{
    if (_lineView3 == nil) {
        _lineView3 = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.surePassWordLabel.frame) + 10, WIDTH - 30, 0.5)];
        _lineView3.backgroundColor = SFMLineColor;
    }
    return _lineView3;
}

-(UIButton *)sureBtn
{
    if (_sureBtn == nil) {
        _sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(42, CGRectGetMaxY(self.lineView3.frame) + 56, WIDTH - 84, 50)];
        _sureBtn.titleLabel.font = font(16);
        [_sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        _sureBtn.layer.cornerRadius = 25;
        [_sureBtn setBackgroundColor:SFMNavBarColor];
        [_sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
